package com.tigercel.xiangchengyun.ui.fragments

import android.os.Bundle
import android.provider.Settings
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.jzvd.JZVideoPlayerStandard
import com.jcodecraeer.xrecyclerview.XRecyclerView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.adapter.VideoSpecialAdapter
import com.tigercel.xiangchengyun.entity.NewsBean
import com.tigercel.xiangchengyun.ui.custom.lbanners.LMBanners
import com.tigercel.xiangchengyun.utils.Global
import com.tigercel.xiangchengyun.utils.JsonUtil
import com.tigercel.xiangchengyun.utils.PreferencesUtil
import kotlinx.android.synthetic.main.fragment_video_special.*
import org.jetbrains.anko.support.v4.toast
import java.text.SimpleDateFormat


class VideoSpecialFragment : Fragment() {
    companion object {

        fun newInstance(channelPosition: Int): VideoSpecialFragment? {
            val fragment = VideoSpecialFragment()
            val bundle = Bundle()
            bundle.putInt("channelPosition", channelPosition)
            fragment.arguments = bundle
            return fragment

        }

    }

    private var containerView: View? = null
    private var banner: LMBanners<Any>? = null
    private var headBanner: View? = null
    private var channelPosition: Int = 0
    private var playerStandard: JZVideoPlayerStandard? = null
    private var contentList1: MutableList<NewsBean> = ArrayList()
    private lateinit var videoAdapter: VideoSpecialAdapter
    private var localDatas: PreferencesUtil? = null
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        containerView = inflater.inflate(R.layout.fragment_video_special, container, false)

        if (arguments != null) {
            channelPosition = arguments!!.getInt("channelPosition")
        }
        return containerView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//        "content_id": 1287,
//        "title": "34岁的新加坡男比赛一天上了57个女人，救护车一旁随时待命！没想到竟...",
//        "thumb_img": "https://gss2.bdstatic.com/5K1IcD3801kUm2zgoZyCKT6zgBYnreHg-_/d1ab4918-ad1d-473f-aaa5-f69cd30210ad.png@w_228|c_1,h_152",
//        "url": "http://video.xcyun.net/2018/0507/70695204d215e67de82d63027fe0856csd/index.mp4",
//        "pub_time": 1497334578
        videoAdapter = VideoSpecialAdapter(ArrayList()) {

        }
        super.onViewCreated(view!!, savedInstanceState)
        recyclerview.layoutManager = LinearLayoutManager(activity)
        recyclerview.adapter = videoAdapter
        recyclerview.refresh()
        localDatas = PreferencesUtil(activity)

        getContent()
//        var news1 = NewsBean("", 1, "习近平:吹响建设科技强国号角国号角国号角", "新华网", "http://video.xcyun.net/2018/0507/70695204d215e67de82d63027fe0856csd/index.mp4", "6小时前", 0, "https://gss2.bdstatic.com/5K1IcD3801kUm2zgoZyCKT6zgBYnreHg-_/d1ab4918-ad1d-473f-aaa5-f69cd30210ad.png@w_228|c_1,h_152", 100)
//        var news2 = NewsBean("", 1, "李志强到我市调研工作", "央视新闻", "www.google.com", "7小时前", 0, "", 200)
//        var news3 = NewsBean("", 1, "改善人民居住环境", "大和网", "www.9527.com", "8小时前", 0, "", 10)
//        var news4 = NewsBean("", 1, "扶贫车间搭建就业脱贫", "人民日报", "www.hahah.com", "9小时前", 0, "", 88)
//        var news5 = NewsBean("", 1, "非法集资案高发", "项城云", "www.eeeeeee.com", "10小时前", 0, "", 999)


//        playerStandard= containerView?.findViewById(R.id.player)
//        playerStandard!!.setAllControlsVisiblity(View.GONE,View.GONE,View.GONE,View.GONE,View.GONE,View.GONE,View.GONE)
//        headBanner = View.inflate(activity, R.layout.banners, null)
//        banner = headBanner?.findViewById(R.id.banners)
//        banner!!.setIndicatorBottomPadding(8)
//        banner!!.setHoriZontalTransitionEffect(TransitionEffect.Default)
//        var bannerBean = Banner(0,"","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1522765537318&di=d6ffbf06f8204eb28510cbc8784918c7&imgtype=0&src=http%3A%2F%2Fi1.hdslb.com%2Fbfs%2Farchive%2Ffbfc3e13a06f6581629970417c0d8ec5286ddfeb.jpg","")
//        var bannerBean2 = Banner(0,"","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1522765809166&di=17b19b778ab60b9d3b214b5c3a256ea0&imgtype=0&src=http%3A%2F%2Fp3.ifengimg.com%2Fa%2F2017_29%2F4f0ea37d91daa59_size103_w1600_h1000.jpg","")
//        var bannerBean3 = Banner(0,"","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1523360415&di=b483f5dab40ad6c0450110601af6dbde&imgtype=jpg&er=1&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farchive%2F64222c045288c88d83cebb57ee71c5bb25780742.jpg","")
//        banner!!.setAdapter(NewsBannerAdapter(activity),listOf(bannerBean,bannerBean2,bannerBean3))
//        recyclerview.addHeaderView(headBanner)
    }

    override fun onDestroy() {
        super.onDestroy()
//        banner!!.clearImageTimerTask()

    }

    override fun onResume() {
        super.onResume()
//        getContent()
    }

    private fun getContent() {
        val devicesId = Settings.System.getString(activity!!.getContentResolver(), Settings.System.ANDROID_ID)
        OkGo.get<String>(Global.baseUrl + "v1/channel/content")
                .tag(this)
                .params("channel_id", channelPosition)
                .params("device_code", devicesId)
                .params("user_id", localDatas!!.preferences["user_id"])
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val content = response!!.body()
                        val status = JsonUtil.get_key_string("status", content!!)
                        val msg = JsonUtil.get_key_string("msg", content!!)
                        val code = JsonUtil.get_key_string("code", content!!)
                        val data = JsonUtil.get_key_string("data", content!!)
                        Log.e("状态参数", status)
                        if (status == "0") {
                            val contentList = JsonUtil.getList("content", data)
                            for (i in 0 until contentList.size) {
                                var content_id = JsonUtil.get_key_string("content_id", contentList[i])
                                var title = JsonUtil.get_key_string("title", contentList[i])
                                var thumb_img = JsonUtil.get_key_string("thumb_img", contentList[i])
                                var url = JsonUtil.get_key_string("url", contentList[i])
                                var readcount = JsonUtil.get_key_string("readcount", contentList[i])
                                var model_name = JsonUtil.get_key_string("model_name", contentList[i])
                                var pub_time = JsonUtil.get_key_string("pub_time", contentList[i]).toLong()
                                var newsBean = NewsBean(content_id, 0, title, "", url, pub_time.toString(), 0, thumb_img, readcount.toInt(), model_name)
                                contentList1.add(newsBean)
                            }
                            Log.e("列表数量", contentList1.size.toString())
                            videoAdapter.addAll(contentList1)

//                            recyclerview.adapter = VideoSpecialAdapter(contentList1) {
//                                toast(it.url)
//                            }
                            recyclerview.refreshComplete()
                            recyclerview.setLoadingListener(object : XRecyclerView.LoadingListener {
                                override fun onRefresh() {
                                    contentList1.clear()
                                    getContent()
                                }

                                override fun onLoadMore() {
                                    getMore()
                                }
                            })
                        } else {
                            toast(getString(R.string.alreadyloadall))
                        }
//
                    }

                })
    }

    private fun getMore() {
        val devicesId = Settings.System.getString(activity!!.getContentResolver(), Settings.System.ANDROID_ID)
        val sdf = SimpleDateFormat("yyyy-MM-dd")
        OkGo.get<String>(Global.baseUrl + "v1/channel/content/page")
                .tag(this)
                .params("channel_id", channelPosition)
                .params("device_code", devicesId)
                .params("page_size", "10")
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val content = response!!.body()
                        val status = JsonUtil.get_key_string("status", content!!)
                        val msg = JsonUtil.get_key_string("msg", content!!)
                        val code = JsonUtil.get_key_string("code", content!!)
                        val data = JsonUtil.getList("data", content!!)
                        if (status == "0") {
                            var news: MutableList<NewsBean>
                            news = ArrayList()
                            if (data.isNotEmpty()) {
                                for (i in 0 until data.size) {
                                    var content_id = JsonUtil.get_key_string("content_id", data[i])
                                    var title = JsonUtil.get_key_string("title", data[i])
//                                    var author = JsonUtil.get_key_string("author", data[i])
                                    var thumb_img = JsonUtil.get_key_string("thumb_img", data[i])
                                    var url = JsonUtil.get_key_string("url", data[i])
                                    var readcount = JsonUtil.get_key_string("readcount", data[i])
                                    var model_name = JsonUtil.get_key_string("model_name", data[i])
                                    var pub_time = JsonUtil.get_key_string("pub_time", data[i]).toLong()
                                    val d = sdf.format(pub_time * 1000L)
                                    var news1 = NewsBean(content_id, 1, title, "", url, d, 0, thumb_img, readcount.toInt(), model_name)
                                    news.add(news1)
                                }
                                videoAdapter.addAll(news)
                                recyclerview.refreshComplete()
                            } else {
                                toast(getString(R.string.alreadyloadall))
                            }
                        } else {
                            toast(getString(R.string.readFaile))
                        }
                    }
                })
    }
}
